package com.chixing.service.impl;

import com.chixing.mapper.CustomerMapper;
import com.chixing.pojo.Customer;
import com.chixing.pojo.CustomerExample;
import com.chixing.service.CustomerService;
//import com.chixing.util.AESUtil;
import com.chixing.util.AESUtil;
import com.chixing.util.RSAUtils;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    private static KeyPair keyPair;
//    private final String key = "EOy6M96s3K1DF3V6JdfBqT3GPtp61ga9";
    @Autowired
    private CustomerMapper customerMapper;


    @Override
    public ServerResponse getById(Integer custId) {
        Customer customer = customerMapper.selectByPrimaryKey(custId);
        if (customer != null) {
            return ServerResponse.getSuccess(customer);
        }
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse save(Customer customer) {
        int insert = customerMapper.insert(customer);
        if(insert >0)
            return  ServerResponse.saveSuccess(customer);
        return ServerResponse.saveFail(null);
    }


    @Override
    public ServerResponse login(Long custTelno , String consumerPassword) {
        System.out.println("注册密码："+consumerPassword);

        String encrypted = consumerPassword;
        String privateKey = getPrivateKey();

        try {
            System.out.println("RSA解密中.....");
            String decrypted = RSAUtils.privateDecrypt(encrypted, RSAUtils.getPrivateKey(privateKey));

            System.out.println("RSA解密后：" + decrypted);
            String encrypt =  AESUtil.encrypt(decrypted);

            System.out.println("AES加密后：" + encrypt);
            consumerPassword = encrypt;

        } catch (Exception e) {
            e.printStackTrace();
        }
        /*try {
            consumerPassword = RSAUtils.encrypt(consumerPassword,RSAUtils.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        System.out.println("consumerPassword:"+consumerPassword);
        CustomerExample customerExample = new CustomerExample();
        customerExample.createCriteria().andCustTelnoEqualTo(custTelno).andCustPasswordEqualTo(consumerPassword);
        List<Customer> customers = customerMapper.selectByExample(customerExample);
        if(customers.size()>0)
            return ServerResponse.getSuccess(customers.get(0));
        return ServerResponse.saveFail(null);
    }

    @Override
    public ServerResponse regist(Long consumerTel) {
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustTelnoEqualTo(consumerTel);


        List<Customer> consumerList = customerMapper.selectByExample(example);
        System.out.println("consumerList.size(): "+consumerList.size());
        if (consumerList.size() > 0) {
            return ServerResponse.getSuccess("existence");
        }
        return ServerResponse.getFail("non-existent");
//        return null;
    }

    @Override
    public ServerResponse save(Long consumerTel , String consumerPassword1) {
        System.out.println("注册密码："+consumerPassword1);

        String encrypted = consumerPassword1;
        String privateKey = getPrivateKey();

        try {
            System.out.println("RSA解密中.....");
            String decrypted = RSAUtils.privateDecrypt(encrypted, RSAUtils.getPrivateKey(privateKey));

            System.out.println("RSA解密后：" + decrypted);
            String encrypt =  AESUtil.encrypt(decrypted);

            System.out.println("AES加密后：" + encrypt);
            consumerPassword1 = encrypt;

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("consumerPassword:"+consumerPassword1);
        Customer consumer = new Customer();
        consumer.setCustTelno(consumerTel);
        consumer.setCustPassword(consumerPassword1);
        int rows = customerMapper.insert(consumer);
        if(rows>0)
            return ServerResponse.saveSuccess(rows);
        return ServerResponse.saveFail(null);
    }

    @Override
    public ServerResponse getPublicKey() {
        try {
            keyPair = RSAUtils.genKeyPair(1024);
            PublicKey publicKey=keyPair.getPublic();
            System.out.println("公钥：");
            String pubKey= new String(Base64.getEncoder().encode(publicKey.getEncoded()));
            System.out.println(pubKey);
            return ServerResponse.getSuccess(pubKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServerResponse.getFail(null);
    }

    @Override
    public String getPrivateKey() {
        try {
            PrivateKey privateKey=keyPair.getPrivate();
            System.out.println("get私钥：");
            String priKey = new String(Base64.getEncoder().encode(privateKey.getEncoded()));



            System.out.println(priKey);
            return priKey;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    @Override
    public Object getPersonCard(MultipartFile multipartFile) {
        return null;
    }
}